﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel.Composition;

using System.ComponentModel.Composition.Hosting;
using SailHero.Soft.FrameWork.Models;
using UI.Web.SL.Demo.SysAdmin.Common;
using System.Json;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;

namespace UI.Web.SL.Demo.SysAdmin
{
    public partial class MainFrame : UserControl
    {
        public MainFrame()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            var lf = new LoginForm();
            lf.Show();
            var hiPage = new HiPage();
            Init(hiPage, "欢迎");
            InitMenu();
        }
        private void left_panel_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var subMenu = left_panel.SelectedItem as MenuInfo;
            if (subMenu == null)
            {
                return;
            }
            var fullName = string.Format("UI.Web.SL.Demo.SysAdmin.{0}"
                ,(string.IsNullOrEmpty(subMenu.Url)?"HiPage":subMenu.Url));
            foreach (var tab in tbControl.Items)
            {
                var t = tab as TabItem;
                if (t.Content == null)
                {
                    return;
                }
                var tfn = t.Content.GetType().FullName;
                if (tfn.Equals(fullName))
                {
                    tbControl.SelectedItem = t;
                    return;
                }
            }
            left_panel.SelectionChanged -= left_panel_SelectionChanged;
            // ViewUtility.CreatePage(fullName);
            var usControl = Application.Current.GetType().Assembly.CreateInstance(fullName);
            var ti = new Controls.PageContainer();
            ti.Header = subMenu.MenuName;
            ti.DataContext = subMenu;
            ti.Content = usControl;
            tbControl.Items.Add(ti);
            tbControl.SelectedItem = ti;
            left_panel.SelectionChanged += left_panel_SelectionChanged;
        }
        private void CloseTabBtnClick(object sender, RoutedEventArgs e)
        {
            DependencyObject dep = (DependencyObject)e.OriginalSource;
            while (dep.GetType() != typeof(TabItem))
            {
                dep = VisualTreeHelper.GetParent(dep);
            }
            tbControl.Items.Remove(dep);
            if (tbControl.Items.Count < 1)
            {
                //Init();
            }
        }

        private void CloseAllClick(object sender, RoutedEventArgs e)
        {
            tbControl.Items.Clear();
            //Init();
        }
        public void Init(UserControl userctl, string title)
        {
            TabItem ti = new TabItem();
            ti.Content = userctl;
            ti.Header = title;
            System.Windows.Style style = (System.Windows.Style)this.Resources["tabItem"];
            ti.Style = style;
            bool CanAdd = true;//判断是否可以添加该TabItem True表示可以  False表示不可以

            foreach (var tab in tbControl.Items)
            {
                var t = tab as TabItem;
                if (t.Header.Equals(ti.Header))
                {
                    tbControl.SelectedItem = t;
                    CanAdd = false;
                    return;
                }
            }

            if (CanAdd == false)
            {
                return;
            }

            tbControl.Items.Add(ti);
            tbControl.SelectedItem = ti;
        }
        private void CloseCurClick(object sender, RoutedEventArgs e)
        {
            tbControl.Items.Remove(tbControl.SelectedItem);
        }

        private void CloseOtherClick(object sender, RoutedEventArgs e)
        {
            var items = tbControl.Items.ToList();
            foreach (var obj in items)
            {
                if (obj == tbControl.SelectedItem)
                {
                    continue;
                }
                else
                {
                    tbControl.Items.Remove(obj);
                }
            }
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChangePSWBtnClick(object sender, RoutedEventArgs e)
        {
            //var win = new PassWordEA();
            //win.Show();
        }
        /// <summary>
        /// 退出登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LoginOutBtnClick(object sender, RoutedEventArgs e)
        {
            ScriptObject so = HtmlPage.Window.GetProperty("CloseWindow") as ScriptObject;
            so.InvokeSelf();
        }
        /// <summary>
        /// 重新登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ReLoginBtnClick(object sender, RoutedEventArgs e)
        {
            HtmlWindow hw = HtmlPage.Window;
            hw.Navigate(new Uri("Default.aspx", UriKind.RelativeOrAbsolute));
        }

        private void tbControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }
      
    }
}
